/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comp.racer.common.so;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 *
 * @author italo
 */
public class Localizador implements Serializable{

    private Integer port;
    private String serviceName;
    private String ip;

    public Localizador(Integer port, String serviceName) throws SocketException {
        this.port = port;
        this.serviceName = serviceName;

        this.ip = obterIpDeRede();
    }


    public Localizador(String ip, Integer port, String serviceName) {
        setIp(ip);
        setPort(port);
        setServiceName(serviceName);
    }

    private String obterIpDeRede() throws SocketException {
        // obtem a lista de interfaces
        Enumeration<NetworkInterface> NetIfaceEnum = NetworkInterface.getNetworkInterfaces();
        while (NetIfaceEnum.hasMoreElements()) {
            NetworkInterface networkInterface = NetIfaceEnum.nextElement();
            // para cada interface, obtem a lista de enderecos
            Enumeration<InetAddress> inetAddressEnum = networkInterface.getInetAddresses();
            while (inetAddressEnum.hasMoreElements()) {
                InetAddress inetAddress = inetAddressEnum.nextElement();
                // para cada endereco, verifica se nao eh endereco de loopback e se o endereco tem
                // tamanho menor que 16
                if (!inetAddress.isLoopbackAddress() && inetAddress.toString().length() <= 16) {
                    // eliminando o "\" no inicio da string
                    return inetAddress.toString().substring(1);
                }
            }
        }

        // por default, se nao houver conexao, retorna o localhost
        return "127.0.0.1";
    }

    // getters e setters
    public String getIp() {
        return ip;
    }

    public void setIp(String clienteIp) {
        this.ip = clienteIp;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer clientePort) {
        this.port = clientePort;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String clienteServiceName) {
        this.serviceName = clienteServiceName;
    }

    @Override
    public String toString() {
        return "rmi://" + getIp() + ":" + getPort() + "/" + getServiceName();
    }

    @Override
    public boolean equals(Object arg0) {

        Localizador localizador = (Localizador) arg0;

        if(localizador.toString().equals( this.toString() ))
            return true;

        return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.port != null ? this.port.hashCode() : 0);
        hash = 53 * hash + (this.serviceName != null ? this.serviceName.hashCode() : 0);
        hash = 53 * hash + (this.ip != null ? this.ip.hashCode() : 0);
        return hash;
    }




}
